package com.tencent.qcloud.timchat.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bxd.baodan.R;
import com.bxd.baodan.activity.AdviserActivity;
import com.tencent.qcloud.timchat.model.Message;
import com.tencent.qcloud.timchat.ui.ChatActivity;

import java.util.List;

/**
 * 聊天界面adapter
 */
public class ChatAdapter extends ArrayAdapter<Message> {

    private final String TAG = "ChatAdapter";
    private String toShowUrl;
    private int resourceId;
    private View view;
    private ViewHolder viewHolder;
    private Context context;

    public String getAgentId() {
        return agentId;
    }

    public void setAgentId(String agentId) {
        this.agentId = agentId;
    }

    private String agentId;

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ChatAdapter(Context context, int resource, List<Message> objects) {
        super(context, resource, objects);
        resourceId = resource;
        this.context = context;
    }

    public String getToShowUrl() {
        return toShowUrl;
    }

    public void setToShowUrl(String toShowUrl) {
        this.toShowUrl = toShowUrl;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null){
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }else{
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.leftMessage = (RelativeLayout) view.findViewById(R.id.leftMessage);
            viewHolder.message_no_read_count = (TextView) view.findViewById(R.id.message_no_read_count);


            viewHolder.rightMessage = (RelativeLayout) view.findViewById(R.id.rightMessage);

            viewHolder.leftPanel = (RelativeLayout) view.findViewById(R.id.leftPanel);
            viewHolder.rightPanel = (RelativeLayout) view.findViewById(R.id.rightPanel);
            viewHolder.sending = (ProgressBar) view.findViewById(R.id.sending);
            viewHolder.error = (ImageView) view.findViewById(R.id.sendError);
            viewHolder.leftAvatar = (ImageView) view.findViewById(R.id.leftAvatar);
            viewHolder.rightAvatar = (ImageView) view.findViewById(R.id.rightAvatar);
            viewHolder.sender = (TextView) view.findViewById(R.id.sender);
            viewHolder.rightDesc = (TextView) view.findViewById(R.id.rightDesc);
            viewHolder.systemMessage = (TextView) view.findViewById(R.id.systemMessage);
            viewHolder.headLy = (LinearLayout) view.findViewById(R.id.header_mag_ly);
            view.setTag(viewHolder);
        }
        viewHolder.headLy.setVisibility(View.GONE);
        viewHolder.leftMessage.setBackground(context.getResources().getDrawable(R.drawable.chat_from_bg_normal));
        viewHolder.rightMessage.setBackground(context.getResources().getDrawable(R.drawable.chat_to_bg_normal));

        viewHolder.leftAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              boolean b =  ((ChatActivity) context).getIntent().getBooleanExtra("isPlaner",false);
                if (!b) {
                    Intent intent = new Intent(context, AdviserActivity.class);
                    intent.putExtra("params",getAgentId());
                    context.startActivity(intent);
                }








            }
        });

        viewHolder.toShowUrl = toShowUrl;
        if (position < getCount()){
            final Message data = getItem(position);
            data.showMessage(viewHolder, context);
        }
        return view;
    }


    public class ViewHolder{
        public String toShowUrl;
        public RelativeLayout leftMessage;
        public RelativeLayout rightMessage;
        public RelativeLayout leftPanel;
        public RelativeLayout rightPanel;
        public ProgressBar sending;
        public ImageView error;
        public ImageView leftAvatar;
        public ImageView rightAvatar;
        public TextView sender;
        public TextView systemMessage;
        public TextView rightDesc;
        public TextView message_no_read_count;
        public LinearLayout headLy;
    }
}
